package service.local.beans;

import java.util.List;

import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Stateless;

import model.Candidate;
import model.Comment;
import repository.CandidateRepository;
import repository.exceptions.RepositoryException;
import service.ServiceException;
import service.local.CandidateService;

@Stateless(name = "CandidateService", mappedName = "CandidateService")
@DependsOn({ "CandidateRepository" })
public class CandidateServiceBean implements CandidateService {

	@EJB
	CandidateRepository repo;

	public Candidate getCandidateByID(Long id) {
		try {
			return this.repo.getById(id);
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void deleteCandidate(Candidate candidate) {
		try {
			repo.delete(candidate);
		} catch (RepositoryException e) {
			e.printStackTrace();
		}

	}

	@Override
	public Candidate updateCandidate(Candidate candidate) {
		try {
			repo.merge(candidate);
			return candidate;
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Candidate> getAll() {
		try {
			return repo.getAll();
		} catch (final RepositoryException ex) {

		}
		return null;
	}

	@Override
	public Candidate insertCandidate(Candidate candidate) {
		try {
			return repo.save(candidate);
		} catch (final RepositoryException ex) {
			// todo
		}
		return null;
	}

	@Override
	public List<Comment> getCommentsForCandidate(Long id) {
		try {
			return repo.getCommentsForCandidate(id);
		} catch (final RepositoryException ex) {

		}
		return null;
	}

	@Override
	public void addCommentToCandidate(Long id, Comment c) {
		try {
			repo.addCommentToCandidate(id, c);
		} catch (final RepositoryException ex) {
			// todo
		}

	}

	@Override
	public Candidate getCandidateByName(String string) {
		try {
			return repo.getCandidateByName(string);
		} catch (final RepositoryException ex) {
			// todo
		}
		return null;
	}

}
